package ipaintplugin.objects;

import ipaintplugin.actions.BaseToolAction;

import java.util.Vector;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.w3c.dom.Node;

//SINGLETON
public class FigureManager {

	private static final String FIGURE_EXTENSION_POINT_ID = "ipaintplugin.figure";
	
	private Vector<Figure> figures;

	private static FigureManager instance = null;

	public static FigureManager getInstance() {
		if (instance == null)
			instance = new FigureManager();

		return instance;
	}

	protected FigureManager() {
		figures = new Vector<Figure>();

		IConfigurationElement[] config = Platform.getExtensionRegistry()
				.getConfigurationElementsFor(FIGURE_EXTENSION_POINT_ID);

		for (IConfigurationElement e : config) {
			try {
				figures.add((Figure) e
						.createExecutableExtension("figureClass"));
			} catch (CoreException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	public Figure getInstanceFromXMLNode(Node n) {
		Figure fig = null;
		for (int i = 0; i < figures.size() && fig == null; i++) {
			Figure f = figures.get(i);
			System.out.println(f.getTagName() + " -- " + n.getNodeName());
			if (f.getTagName().compareToIgnoreCase(n.getNodeName()) == 0) {
				System.out.println(n);
				fig = (Figure) f.clone();
				fig.fromXML(n);
			}
		}
		return fig;
	}
}
